package com.ijiuchuang.jc_localization.tools.views;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.AppCompatTextView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

/**
 * Created by 王伟 on 2016/6/28
 * Function : 评论回复控件
 */
public class CommentView extends AppCompatTextView {

    private Context context;
    private String userA = "张三";
    private String userB = "李四";
    private String commentText = "哈哈哈哈";
    private int nameColor = 0xff576b95;
    private int defaultColor = 0xff000333;

    private TextView textView;

    public CommentView(Context context) {
        this(context, null);
    }

    public CommentView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommentView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public static Builder with(CommentView commentView) {
        return new Builder(commentView);
    }

    public static Builder with(TextView textView) {
        return new Builder(textView);
    }

    public static final class Builder {
        private String userA;
        private String userB;
        private String commentText;

        private CommentView commentView;
        private TextView textView;

        public Builder(CommentView commentView) {
            this.commentView = commentView;
        }

        public Builder(TextView textView) {
            this.textView = textView;
        }

        public Builder setUserA(String userA) {
            this.userA = userA;
            return this;
        }

        public Builder setUserB(String userB) {
            this.userB = userB;
            return this;
        }

        public Builder setCommentText(String commentText) {
            this.commentText = commentText;
            return this;
        }

        public void show(CommentClickListener listener){
            commentView.setUserA(userA);
            commentView.setUserB(userB);
            commentView.setCommentText(commentText);
            commentView.show(listener);
        }
    }

    public void setUserA(String userA) {
        this.userA = userA;
    }

    public void setUserB(String userB) {
        this.userB = userB;
    }

    public void setCommentText(String commentText) {
        this.commentText = commentText;
    }

    public void show(CommentClickListener listener) {
        setTextColor(defaultColor);
        setHighlightColor(Color.TRANSPARENT);
        setMovementMethod(LinkMovementMethod.getInstance());
        /*setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("CommentView is click!");
            }
        });*/

        SpannableString spanText1 = new SpannableString(userA + "回复" + userB + "：" + commentText);
        spanText1.setSpan(
                new ForegroundColorSpan(nameColor),
                0,
                userA.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
        spanText1.setSpan(
                new ForegroundColorSpan(nameColor),
                (userA + "回复").length(),
                (userA + "回复" + userB).length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
        spanText1.setSpan(new OnUserNameClick(userA, OnUserNameClick.USER_A_CLICK, listener), 0, userA.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanText1.setSpan(new OnUserNameClick(userB, OnUserNameClick.USER_B_CLICK, listener), (userA + "回复").length(), (userA + "回复" + userB).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanText1.setSpan(new OnUserNameClick(commentText, OnUserNameClick.COMMENT_CLICK, listener), (userA + "回复" + userB + "：").length(), spanText1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        setText(spanText1);
    }

    public class OnUserNameClick extends ClickableSpan {

        private CommentClickListener listener;
        private String text;
        private int state;

        public static final int USER_A_CLICK = 0;
        public static final int USER_B_CLICK = 1;
        public static final int COMMENT_CLICK = 2;

        public OnUserNameClick(String text, int state, CommentClickListener listener) {
            this.listener = listener;
            this.text = text;
            this.state = state;
        }

        @Override
        public void onClick(View widget) {
            switch (state) {
                case USER_A_CLICK:
                    listener.onUserAClick(text);
                    break;
                case USER_B_CLICK:
                    listener.onUserBClick(text);
                    break;
                case COMMENT_CLICK:
                    listener.onCommentClick(text);
                    break;
            }
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);
            if (state == COMMENT_CLICK) {
                ds.setColor(defaultColor);
            } else {
                ds.setColor(nameColor);
            }
        }
    }

    public interface CommentClickListener {
        void onUserAClick(String userA);
        void onUserBClick(String userB);
        void onCommentClick(String comment);
    }
}
